package services;

import java.sql.Connection;

import com.example.maze.ConnectionInfo;

import tw.edu.tku.im.maze.DeliveryListenerI;
import tw.edu.tku.im.maze.EnterRoomDeliverer;
import tw.edu.tku.im.maze.EnterRoomReq;
import tw.edu.tku.im.maze.RepPacket;
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

public class JoinTheGameService extends IntentService
                                implements DeliveryListenerI{
   public static final String BROADCAST_ACTION= "JoinTheGame";

   public JoinTheGameService()            { super("JoinTheGameService"); }
   public JoinTheGameService(String name) { super(name); }

   @Override
   protected void onHandleIntent(Intent intent) {
      ConnectionInfo.roomId= intent.getIntExtra("roomId", -1);
      try {
         EnterRoomDeliverer mEnterRoomDeliverer= new EnterRoomDeliverer();
         mEnterRoomDeliverer.deliver(new EnterRoomReq(intent.getIntExtra("roomId", -1),
                                                      ConnectionInfo.id,
                                                      ConnectionInfo.ticket,
                                                      ""), 
                                                      this);
      } catch(Exception e) {}      
   }

   @Override
   public void processReply(RepPacket rep) {
      Intent localIntent= new Intent(BROADCAST_ACTION);
      localIntent.putExtra("packet", rep);
      LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
   }
}
